/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-06-30 10:09:43
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-07-18 10:05:00
 * @FilePath: /XMate-h5/src/store/modules/minutes/utils/asrProcessing.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  AsrObject,
  AsrParagraphs,
  UidInfo,
  CustomerRoles,
  ParticipantResult,
  RecordDetailInfo,
  InternalPartner
} from "@/types/minutes/store.type";
import { getUserName } from "./userNameResolver";
import { timeAddTS, timeAddTSByFile} from "@/utils/timeTool"

/**
 * 生成AI优化过的字幕列表
 * @param asrParagraphsList ASR段落列表
 * @param uidInfo 用户信息映射
 * @param customerRoles 客户角色数据
 * @param participantResults 参与者结果数据
 * @returns AI优化过的字幕列表
 */
export function generateAiAsrList(
  asrParagraphsList: AsrParagraphs[],
  uidInfo: Record<string, UidInfo> = {},
  customerRoles: CustomerRoles[] = [],
  internalPartners: InternalPartner[] = [],
  participantResults: ParticipantResult[] = [],
  recordInfo: RecordDetailInfo
): AsrParagraphs[] {
  return (asrParagraphsList || []).map((v: AsrParagraphs) => {
    v.name = getUserName(v.ui, uidInfo, customerRoles, internalPartners, participantResults);
    v.txt = v.AsrList?.map((item: AsrObject) => item.txt).join("") || "";
    v.bt = v.AsrList?.[0]?.bt || 0;
    v.startTime = timeAddTS(recordInfo.startTime, v.bt);
    v.startTimeByFile = timeAddTSByFile(v.bt);
    return v;
  });
}

/**
 * 生成原始字幕列表
 * @param asrParagraphsList ASR段落列表
 * @param uidInfo 用户信息映射
 * @param customerRoles 客户角色数据
 * @param participantResults 参与者结果数据
 * @returns 原始字幕列表
 */
export function generateAsrList(
  asrParagraphsList: AsrParagraphs[],
  uidInfo: Record<string, UidInfo> = {},
  customerRoles: CustomerRoles[] = [],
  internalPartners: InternalPartner[] = [],
  participantResults: ParticipantResult[] = [],
  recordInfo: RecordDetailInfo
): AsrObject[] {
  const aiAsrList = generateAiAsrList(
    asrParagraphsList,
    uidInfo,
    customerRoles,
    internalPartners,
    participantResults,
    recordInfo
  );

  return aiAsrList.reduce((acc: Array<AsrObject>, item: AsrParagraphs) => {
    if (!item.AsrList) {
      // 兼容后端异常数据
      return acc;
    }
    item.AsrList.forEach((v: AsrObject) => {
      v.name = item.name;
      v.ui = item.ui;
      v.duration = v.et - v.bt;
      v.startTime = timeAddTS(recordInfo.startTime, v.bt);
      v.startTimeByFile = timeAddTSByFile(v.bt);
    });
    acc.push(...item.AsrList);
    return acc;
  }, [] as Array<AsrObject>);
}
